package com.willy.ratingbar;

import ohos.multimodalinput.event.TouchEvent;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/**
 * Created by willy on 2018/3/8.
 */

class RatingBarUtils {

    private static DecimalFormat mDecimalFormat;
    private static final int MAX_CLICK_DISTANCE = 5;
    private static final int MAX_CLICK_DURATION = 200;

    static boolean isClickEvent(float startX, float startY, TouchEvent event) {
        float duration = event.getStartTime() - event.getOccurredTime();
        if (duration > MAX_CLICK_DURATION) {
            return false;
        }
        BigDecimal bignum1 = new BigDecimal(startX);
        BigDecimal bignum2 = new BigDecimal(event.getPointerPosition(0).getX());
        float floatValue = bignum1.subtract(bignum2).floatValue();
        float differenceX = Math.abs(floatValue);
        BigDecimal bignum3 = new BigDecimal(startY);
        BigDecimal bignum4 = new BigDecimal(event.getPointerPosition(0).getY());
        float floatValue2 = bignum3.subtract(bignum4).floatValue();
        float differenceY = Math.abs(floatValue2);
        return !(differenceX > MAX_CLICK_DISTANCE || differenceY > MAX_CLICK_DISTANCE);
    }

    static float calculateRating(PartialView partialView, float stepSize, float eventX) {
        DecimalFormat decimalFormat = RatingBarUtils.getDecimalFormat();
        BigDecimal bignum1 = new BigDecimal(eventX);
        BigDecimal bignum2 = new BigDecimal(partialView.getLeft());
        float value = bignum1.subtract(bignum2).floatValue();
        float ratioOfView = Float.parseFloat(decimalFormat.format((value) / partialView.getWidth()));
        float steps = Math.round(ratioOfView / stepSize) * stepSize;
        BigDecimal bignum3 = new BigDecimal(1);
        BigDecimal bignum4 = new BigDecimal(steps);
        BigDecimal subtract = bignum3.subtract(bignum4);
        BigDecimal bignum5 = new BigDecimal((int) partialView.getTag());
        float value1 = bignum5.subtract(subtract).floatValue();
        return Float.parseFloat(decimalFormat.format(value1));
    }

    static float getValidMinimumStars(float minimumStars, int numStars, float stepSize) {
        if (minimumStars < 0) {
            minimumStars = 0;
        }

        if (minimumStars > numStars) {
            minimumStars = numStars;
        }

        BigDecimal decimal1 = new BigDecimal(minimumStars);
        BigDecimal decimal2 = new BigDecimal(stepSize);
        BigDecimal[] cc = decimal1.divideAndRemainder(decimal2);
        if (cc[1].floatValue() != 0) {
            minimumStars = stepSize;
        }
        return minimumStars;
    }

    static DecimalFormat getDecimalFormat() {
        if (mDecimalFormat == null) {
            DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH);
            symbols.setDecimalSeparator('.');
            mDecimalFormat = new DecimalFormat("#.##", symbols);
        }
        return mDecimalFormat;
    }
}
